/* @(#)GSOrderDetailSubmitQueryAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-3-17     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import system.LoginContext;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.form.GSOrderDetailSubmitQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderSubmitToApepQueryVO;

/**
 * 
 */
public class GSOrderDetailSubmitQueryAction extends GSBaseAction {


    public String execute(GSBaseForm aForm) throws Exception {
        //取得页面参数放入VO对象
        GSOrderDetailSubmitQueryForm tForm = (GSOrderDetailSubmitQueryForm)aForm;
        GSOrderSubmitToApepQueryVO tVO = new GSOrderSubmitToApepQueryVO();
        tVO.orderDetailId = tForm.orderDetailId;
        tVO.providerName = tForm.providerName;
        tVO.orderDeptId = tForm.orderDeptId;
        tVO.deliverGoodsAddrId = tForm.deliverGoodsAddrId;
        tVO.concentricityId = tForm.concentricityId;
        tVO.manufInfo = tForm.manufInfo;
        tVO.manuftype1 = tForm.manuftype1;
        tVO.manuftype2 = tForm.manuftype2;
        tVO.manuftype3 = tForm.manuftype3;
        tVO.drawingpiececode = tForm.drawingpiececode;
        String[] tDateStr = new String[2];
        tDateStr[0] = tForm.orderCreateDateStr;
        tDateStr[1] = tForm.orderCreateDateEnd;
        tDateStr = DateUtil.formateDate(tDateStr);
        tVO.orderCreateDateStr = tDateStr[0];
        tVO.orderCreateDateEnd = tDateStr[1];
        try{
            LoginContext tOperBean = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            GSUniteGwProcessor processer = new GSUniteGwProcessor();
            ArrayList ary = processer.getWareHouseLevelName(tOperBean.getLoginWarehouseCode(),tOperBean.getLoginWarehouseCode());
            aForm.getRequest().setAttribute("organdaddr",ary);
            GSManufactureProcessor mproc = new GSManufactureProcessor();
            mproc.getManufactureOfLevel(aForm);
            //取得分页参数
            String[] tStr = this.getPageParams(aForm,tVO);
            ArrayList tList = new ArrayList();
            GSOrderProcessor tProc = new GSOrderProcessor();
            tList = tProc.getOrderAndOrderDetail(tVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            if(tList.size() > 0){
                int tSize = tList.size() - 1;
                String totalAmt = (String)tList.get(tSize);
                tForm.getRequest().setAttribute("totalAmt",totalAmt);
                tList.remove(tSize);
            }
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(tList);
            tForm.getRequest().setAttribute("order",tRsProc);
            
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E00001,GSException.E00001_MSG);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    private String[] getPageParams(GSBaseForm aForm,GSOrderSubmitToApepQueryVO aOrderVO) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            GSOrderDetailDAO tDAO = new GSOrderDetailDAO();
            totalRecords = tDAO.queryAllAuthoriseOrdersCount(aOrderVO);
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }

}
